import {BeanScope} from '../beans/BeanScope';
import {StringUtils} from '../util/StringUtils';
import {DecoratorMetadata} from './meta/DecoratorMetadata';
import {createClassDecorator} from './util';


/**
 * 保存于类上的metadata
 */
export interface ComponentMetadata extends DecoratorMetadata {
  /**
   * bean name
   */
  name: string;
  /**
   * bean scope
   */
  scope: BeanScope
}


/**
 * Bean声明
 *
 * @param name Bean名称
 *
 * @author Zhang
 * @date 2019-5-23 18:06:10
 */
export function Component(name?: string): ClassDecorator;
/**
 * Bean声明
 * @param opt
 */
export function Component(opt: {
  /**
   * Bean名称
   */
  name?: string,
  /**
   * Bean scope
   */
  scope?: BeanScope
}): ClassDecorator;


export function Component(opt): ClassDecorator {
  return createClassDecorator(target => {
    let name: string, scope: BeanScope;
    if (typeof opt === 'string') {
      name = opt;
      scope = BeanScope.SINGLETON;
    } else {
      name = (opt && opt.name) || StringUtils.firstLetter2Lower(target.name);
      scope = (opt && opt.scope) || BeanScope.SINGLETON;
    }
    const compMeta: ComponentMetadata = {
      decorator: Component,
      name,
      scope,
    };
    return compMeta;
  });
}
